<?php require_once(dirname(__FILE__)."/config/config.php"); ?>
<?php require 'info.php';?>
 <?php include (dirname(__FILE__)."/include/header.php"); ?>
 <?php  
if($_SESSION['_cart']['food']=="")
{

	echo "<script>location.href='cart_empty.php';</script>";
}
$a=totalPrice();
// echo $a;
if($a<=0){
	echo "<script>location.href='cart_empty.php';</script>";
}

?>
<?php
if($a!=0){
//保存购物车数据进数据库.并清空购物车
if($_REQUEST['method']=='saveCart'){
	$shop_id=$_SESSION['_cart']['shop_id'];
	$sql="select * from zy_shop where shop_id=".$shop_id;
	//echo $sql;
	$result=mysql_query($sql);
	$zy_shop=mysql_fetch_array($result);
	$totalPrice=totalPrice();
    $total = $totalPrice + $zy_shop['surcharge'];
    $addintegral=$total*10;
	$add_id=$_REQUEST['add_id'];
	$remark=$_REQUEST['remark'];
	$tel=$_REQUEST['tel'];
	$phone=$_REQUEST['phone'];
	$address=$_REQUEST['address'];
	
	if(isset($_SESSION['_user'])){
		$user_id=$_SESSION['_user']['user_id'];
		$user_name=$_SESSION['_user']['username'];
		//判断用户是登录用户,已登录状态
		if($add_id==""){
			//add_id为空时,为新地址.
		}
	}else{
		//判断用户是游客,未登录状态
		$user_id=0;
		$user_name="游客";
	}
    $sql5="UPDATE zy_user  SET  integral = integral+ '".$addintegral."' WHERE user_id =".$_SESSION['_user']['user_id'];
    mysql_query($sql5);
	$sql="INSERT INTO `zy_order`(`shop_id`,`user_id`,`add_id`,`totalPrice`,`order_time`,`order_status`,`remark`,`is_delete`,`create_user`,`create_time`,`update_user`,`update_time`) VALUES ('$shop_id','$user_id','$add_id','$totalPrice',NOW(),'0','$remark','0','$user_name',NOW(),'$user_name',NOW())";
	mysql_query($sql);
	$order_id=mysql_insert_id ();
	if($add_id==""&&$user_id==0){
		//add_id为空,并且是游客状态下.地址表里记录的user_id就是点单的ID.并且创建人也是点单的ID.
		$sql="insert into zy_address(user_id,address,tel,phone,create_user,create_time,update_user,update_time) " .
				"values('$order_id','$address','$tel','$phone','$order_id',now(),'$order_id',now())";
		mysql_query($sql);
	}else if($add_id==""&&$user_id!=0){
		//add_id为空,并且是用户已登录状态下.新增一条用户地址.
		$sql="insert into zy_address(user_id,address,tel,phone,create_user,create_time,update_user,update_time) " .
				"values('$user_id','$address','$tel','$phone','$user_name',now(),'$user_name',now())";
		mysql_query($sql);
	}
	//插入数据到点单详细表中
	foreach($_SESSION['_cart']['food'] as $food){
		$sql="INSERT INTO `zy_item`(`order_id`,`food_id`,`food_name`,`food_price`,`count`,`subtotal`,`remark`,`is_delete`,`create_user`,`create_time`,`update_user`,`update_time`) 
				VALUES ('$order_id','".$food['food_id']."','".$food['food_name']."','".$food['price']."','".$food['quantity']."','".$food['price']*$food['quantity']."','','0','$user_name',now(),'$user_name',now());";
		mysql_query($sql);
		$order_item.="<tr><th>".$food['food_name']."</th>"."<th>".$food['quantity']."</th>"."<th>".$food['price']."</th>"."<th>".$food['price']*$food['quantity']."</th>"."</tr>";
       
	}
		if($_SESSION['_user']['username']!=""){
       $user=$_SESSION['_user']['username'];
    }else{
		$user='游客';
    }
	
	/***************************邮件配置************************************/
	$mail = new PHPMailer(); //建立邮件发送类
	$address = $_POST['address']; 
	$mail->IsSMTP(); // 使用SMTP方式发送
	$mail->Host = "smtp.exmail.qq.com"; // 您的企业邮局域名
	$mail->SMTPAuth = true; // 启用SMTP验证功能
	$mail->Username = "sendmail@dianexia.com "; // 邮局用户名(请填写完整的email地址)
	$mail->Password = "dyx123456"; // 邮局密码
	
	$mail->From = "sendmail@dianexia.com"; //邮件发送者email地址
	$mail->FromName = "sendmail@dianexia.com ";
    $mail->AddAddress("149981331@qq.com", "149981331@qq.com");
    $mail->AddAddress("order@dianexia.com", "order@dianexia.com");
	$mail->AddAddress("shulei@maszy.cn", "束总");
   // $mail->AddAddress("order@dianexia.com", "order@dianexia.com");
	//收件人地址，可以替换成任何想要接收邮件的email信箱,格式是AddAddress("收件人email","收件人姓名")
	//$mail->AddReplyTo("", "");
	
	//$mail->AddAttachment("/var/tmp/file.tar.gz"); // 添加附件
	$mail->IsHTML(true); // set email format to HTML //是否使用HTML格式
	
	$mail->Subject = "点单通知"; //邮件标题
	/***************************邮件配置************************************/
	
	$sql2="select * from zy_order where ord_id=".$order_id;
    $result2=mysql_query($sql2);
    $zy_order=mysql_fetch_array($result2);
	$sql3="select * from zy_item where order_id = ".$order_id;
	//echo $sql3;
	$result3 = mysql_query($sql3);
	$address = $_REQUEST['address'];
	$phone = $_REQUEST['phone'];
	$site=SITE_ROOT;
    //$time = $zy_order['order_time'];
    $user1="<tr><th>".用户名."</th>"."<th>".$user."</th><tr>";
    $site1="<tr><th>".点单网址."</th>"."<th>".$site."</th><tr>";
    $time="<tr><th>".点单时间."</th>"."<th>".$zy_order['order_time']."</th><tr>";
    $orderid="<tr><th>".点单号."</th>"."<th>".$order_id."</th><tr>";
    $total1="<tr><th>".总金额."</th>"."<th>".$total."</th><tr>";
    $address1="<tr><th>".配送地址."</th>"."<th>".$address."</th><tr>";
    $phone1="<tr><th>".联系电话."</th>"."<th>".$phone."</th><tr>";
	$shopname="<tr><th>".店铺名称."</th>"."<th>".$zy_shop['shop_name']."</th><tr>";
	$surcharge="<tr><th>".外送费."</th>"."<th>".$zy_shop['surcharge']."</th><tr>";
$mail->Body = "

	
<html>
<head>
  <title>点单通知</title>
</head>
<body>
  <p>点单通知表</p>
  <table boder='1'>
        {$user1}
        {$site1}
        {$time}
        {$orderid}
        {$total1}
        {$address1}
        {$phone1}
		{$shopname}
		{$surcharge}
	<tr>
	<th>食品名称</th>
	<th>数量</th>
	<th>单价</th>
	<th>金额</th>
	</tr>
	{$order_item}
	</table>
</body>
</html>
"; 
 $mail->Send();//发送邮件
}
}
?>
<!DOCTYPE html>
<html lang="zh">
<head>
<title>点一下,宁波最大的外卖订餐网站!</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="keywords" content="外卖,网上订餐,餐厅,美食" />
<meta name="description" content="查看附近的餐厅、美食信息" />
<meta http-equiv="X-UA-Compatible" content="chrome=IE6">
<script type="text/javascript" src="<?php echo SITE_ROOT;?>/js/jquery-1.8.0.js"></script>
<script type="text/javascript" src="<?php echo SITE_ROOT;?>/js/bootstrap/js/bootstrap.min.js"></script>
</head>
<body>
<div id="background"></div>
<div class="full-content-wrapper">
  <div class="cart-container">
    <div class="row cart-wrapper">
      <div class="span9 cart-header">
        <div class="cart-nav">
           <!-- 此处是为了不让字把云遮住 --> 
        </div>
      </div>
      <div class="span9 flow">
        <ul>
          <li>1、选择美食 →</li>
          <li>2、填写核对点单信息 →</li>
          <li class="current">3、成功提交点单</li>
        </ul>
      </div>
      <div class="span9 main-box bordered-box">
        <div class="order-result-box">
          <div class="line-one"> <span class="status-icon"></span>
            <h3>点单提交成功</h3>
            <!--<button class="btn btn-yellow" id="shuiqunawaimai_open">谁去拿外卖</button> -->
          </div>
          <div class="line-two">
            <p>点单号码: <span class="detail"><?php echo $order_id?></span></p>
            <p>总计金额: <span class="detail"><?php echo $total;?>元</span></p>
            <p>餐厅名称: <span class="detail"><?php echo $_SESSION['_cart']['shop_name']?></span></p>
            <p>配送地址: <span class="detail"><?php echo $_REQUEST['address'];?></span></p>
            <p>联系电话: <span class="detail"><?php echo $_REQUEST['phone'];?>,<?php echo $_REQUEST['tel'];?></span></p>
            <?php if($_REQUEST['remark']!=""){?>
             <p>点单备注: <span class="detail"><?php echo $_REQUEST['remark'];?></span></p>
             <?php } ?>
          </div>
          <div class="line-three">
            <p class="confirm_text"> 请保持电话<?php echo $_REQUEST['phone'];?>,<?php echo $_REQUEST['tel'];?>开机。 </p>
            <p class="confirm_text"> 注册用户点评美食可拿积分，积分可兑换超值好礼！（1元=10分） </p>
             <p class="confirm_text" align="right">
			<a href="<?php echo SITE_ROOT."/gift"?>">礼品中心</a> &nbsp;&nbsp;
			<?php if(!isset($_SESSION['_user'])){?>
			<a href="<?php echo SITE_ROOT."/user_registration.php"?>">现在注册</a> &nbsp;&nbsp;
			<?php }else{?>
			<a href="<?php echo SITE_ROOT."/user/order_list.php?method=unrated"?>">现在点评</a> &nbsp;&nbsp;
			<?php }?>
			</p>
          </div>
        </div>
    </div>
  </div>
  <?php 
  //点单提交成功后去除购物车里的信息
  unset($_SESSION['_cart']); ?>
  <script type="text/javascript">
function openwin(){	
	if(navigator.appName=="Microsoft Internet Explorer"){	
		window.showModalDialog('http://e.weibo.com/2963113320/messboard','','dialogWidth=900px;dialogHeight=600px');	
	}
	else if(navigator.appName=="Netscape"){		
		window.open('http://e.weibo.com/2963113320/messboard','','width=900,height=600,modal=yes,location=no,status=no,toolbar=no,resizble=yes'); 
	}	
}

</script>
  <?php include (dirname(__FILE__)."/include/footer.php"); ?>
  
